<template>
  <div class="login-container">
    <!-- <div class="login-brand">
      <p class="bgp1-1">生产中心项目管理系统</p>
    </div> -->
    <div class="title1">
      <p class="title_home">陕西省水利专家管理平台</p>
      <img class="img" src="../../assets/u374.png" />
    </div>
    <!-- <div class="title">

      </div> -->
    <div class="loginbox">
      <dv-border-box-9>
        <!-- <img src="../../assets/loginbox.png" alt=""> -->
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="right"
        >
          <!-- <div style="background: url(../../assets/loginbox.png);">
  123
</div> -->
          <div class="title">用户登陆</div>
          <!-- <div class="title-container">

        </div> -->

          <el-form-item style="margin-left: 4%" prop="username">
            <!-- <span class="svg-container">
            <svg-icon icon-class="user" />
          </span> -->
            <el-input
              ref="username"
              style="width: 95%; margin-top: 2%"
              v-model="loginForm.username"
              placeholder="请输入用户名称"
              name="username"
              class="transparent-input"
              type="text"
            />
          </el-form-item>

          <el-form-item style="margin-left: 4%" prop="password">
            <!-- <span class="svg-container">
            <svg-icon icon-class="password" />
          </span> -->
            <el-input
              :key="passwordType"
              ref="password"
              style="width: 95%; margin-top: 2%"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入登录密码"
              name="password"
              tabindex="2"
              class="transparent-input"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
          <el-form-item style="margin-left: 4%" prop="captcha">
            <el-row :gutter="20">
              <el-col :span="14">
                <el-input
                  v-model="loginForm.captcha"
                  placeholder="请输入验证码"
                  class="transparent-input"
                  style="width: 95%; margin-top: 2%"
                >
                  <!-- <span slot="prefix" class="el-input__icon">
                      <svg class="icon-svg" aria-hidden="true"><use xlink:href="#icon-safetycertificate"></use></svg>
                    </span> -->
                </el-input>
              </el-col>
              <el-col :span="10" class="login-captcha">
                <img :src="captchaPath" style="width: 85%;" @click="getCaptcha()" />
              </el-col>
            </el-row>
          </el-form-item>
          <!--
          <el-form-item prop="password">
            <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
            <el-input
              :key="passwordType"
              ref="password"
              style="width: 90%; margin-left: 5%"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入登录密码"
              name="password"
              tabindex="2"
              class="transparent-input"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item> -->

          <el-button
            :loading="loading"
            type="primary"
            style="
              width: 92%;
              margin-bottom: 10%;
              margin-left: 4%;
            "
            @click.native.prevent="handleLogin"
            >登录</el-button
          >

          <!-- <div class="tips">
          <span style="margin-right: 20px">username: admin</span>
          <span> password: any</span>
        </div> -->
        </el-form>
      </dv-border-box-9>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import { getUUID } from "@/api/user";
import { login,getpic } from "@/api/user";
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("账号不能为空！"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位！"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
        captcha: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
        ],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      captchaPath: "",
    };
  },
  created() {
    this.getCaptcha();
  },
  watch: {
    $route: {
      handler: function (route) {
        console.log(route); // this.redirect = route.query && route.query.redirect

        this.redirect = "routerpage";
        console.log(this.redirect);
      },
      immediate: true,
    },
  },
  methods: {
    getCaptcha() {
      console.log(123);
      const timestamp = new Date().getTime(); // 防止缓存
      getpic(timestamp).then((res) => {
        console.log(res);
        console.log(typeof(res));
        this.captchaPath = URL.createObjectURL(res);
      })
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    login() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          // this.$router.push("/Screen");
          // login(this.loginForm).then((res) => {
          //   console.log(res);
          // })
          http
            .post("/dev-api/admin/system/index/login", this.loginForm, {
              headers: {
                "Content-Type": "application/json",
              },
            })
            .then((res) => {
              console.log(res);
              sessionStorage.setItem('token',res.data.token)
              this.$message({
                message: "登陆成功！",
                type: "success",
              });
              // this.$router.push("/Screen");
            });
        }
      });
    },
    handleLogin() {
      login(this.loginForm).then((res) => {
        console.log(res);
        if (res.code == 200) {
          console.log(res);

              sessionStorage.setItem('token',res.data.token)
              sessionStorage.setItem('role',res.data.RoleCode)
          this.$message({
            message: "登陆成功！",
            type: "success",
          });
          this.$router.push("/routerpage");
        } else if (res.code == 20001) {
          this.$message({
            message: "验证码错误！",
            type: "danger",
          });
        }
      });
    },
  },
};
</script>

<!-- <style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .loginbox {
    height: 50%;
    width: 40%;
    position: relative; /* 关键：为伪元素定位提供基准 */
    z-index: 1; /* 确保内容在伪元素之上 */
  }
  .loginbox::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url(../../assets/loginbox.png) center/cover; /* 设置背景图 */
    opacity: 0.3; /* 单独控制背景图透明度 */
    z-index: -1; /* 将伪元素置于内容下方 */
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style> -->

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #fff;
::v-deep.transparent-input .el-input__inner {
  background-color: rgba(255, 255, 255, 0); /* 设置透明背景 */
  color: #fff;
  border: 1px solid #37b1f4; /* 设置边框颜色 */
}
.login-container {
  padding-top: 7%;
  min-height: 100%;
  width: 100%;
  // background-color: white;
  background: url(../../assets/loginbg.png);
  overflow: hidden;
  .loginbox {
    color: white;
    height: 50%;
    width: 25%;
    margin-top: 10%;
    margin-left: 37%;
    position: relative; /* 关键：为伪元素定位提供基准 */
    z-index: 1; /* 确保内容在伪元素之上 */
  }
  .login-brand {
    text-align: center;
    color: #fff;
    margin: 0 0 15px;
    font-size: 70px;
    font-weight: bolder;
    letter-spacing: 10px;
    text-transform: uppercase;
  }
  // .loginbox::before {
  //   content: "";
  //   position: absolute;
  //   top: 0;
  //   left: 10%;
  //   width: 100%;
  //   height: 100%;
  //   background: url(../../assets/loginbox.png) center/cover; /* 设置背景图 */
  //   opacity: 0.5; /* 单独控制背景图透明度 */
  //   z-index: -1; /* 将伪元素置于内容下方 */
  // }
  .login-form {
    // position: relative;
    width: 90%;
    height: 100%;
    // padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  // .title-container {
  //   position: relative;

  //   .title {
  //     font-size: 1.5vw;
  //     color: $light_gray;

  //     margin: 0px auto 40px auto;
  //     text-align: center;
  //     font-weight: bold;
  //   }
  // }
  .title {
    font-size: 1.4vw;
    color: $light_gray;
    text-align: left;
    font-weight: bold;
    margin-top: 8%;
    text-align: center;
  }
  .show-pwd {
    position: absolute;
    right: 7%;
    top: 25%;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .title1 {
    border-width: 0px;
    position: absolute;

    top: 0%;
    width: 100%;
    height: 15%;

    line-height: 0px;
    text-align: center; /* 文本居中 */
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .title1 img {
    border-width: 0px;
    position: relative;
    left: 0px;
    top: -20px;
    width: 100%;
    height: 80%;

    z-index: 1;
  }

  .title_home {
    position: absolute;
    font-size: 2vw;
    top: 7%;
    left: 50%;
    transform: translate(-50%, -50%); /* 将文本居中 */
    color: rgb(255, 255, 255);
    font-weight: bolder;
    z-index: 2;
  }
  .title_text {
    position: absolute;
    top: 26%;
    left: 38%;
    width: 20%;
    line-height: 28px;
    font-size: 24px;
    color: rgb(255, 255, 255);
    z-index: 0;
  }
}
</style>
